package com.foreverwin.mes.integration.soap.materialissue.service.impl;

import com.foreverwin.mes.integration.enums.ResponseStatus;
import com.foreverwin.mes.integration.soap.material.dto.WebServiceResponse;
import com.foreverwin.mes.integration.soap.materialissue.dto.MaterialIssueRequest;
import com.foreverwin.mes.integration.soap.materialissue.service.MaterialIssueService;
import com.foreverwin.mes.warehouse.dto.InventoryReceiveDto;
import com.foreverwin.mes.warehouse.service.InventoryReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ervin Chen
 * @date 2020/1/13 17:37
 */
@WebService(serviceName = "MaterialIssueService",//对外发布的服务名
        //指定你想要的名称空间，通常使用使用包名反转
        targetNamespace = "http://service.meint.djhs.com",
        //服务接口全路径, 指定做SEI（Service EndPoint Interface）服务端点接口
        endpointInterface = "com.foreverwin.mes.integration.soap.materialissue.service.MaterialIssueService")
@Component
public class MaterialIssueServiceImpl implements MaterialIssueService {

    @Autowired
    private InventoryReceiveService inventoryReceiveService;

    @Override
    public @WebResult(name = "issueResponse")
    WebServiceResponse issueMatierl(@WebParam(name = "issueRequest") MaterialIssueRequest issueRequest) {
        WebServiceResponse response = new WebServiceResponse();
        response.setStatus(ResponseStatus.S);
        response.setMessage(issueRequest.getDocumentNo());

        List<InventoryReceiveDto> inventoryReceiveDtos = issueRequest.getDocumentItems()
                .stream()
                .map(documentItem -> {
                    InventoryReceiveDto inventoryReceiveDto = new InventoryReceiveDto();
                    inventoryReceiveDto.setSendTime(issueRequest.getTransactionTime());
                    inventoryReceiveDto.setDocumentNo(issueRequest.getDocumentNo());
                    inventoryReceiveDto.setDocumentYear(issueRequest.getDocumentYear());
                    inventoryReceiveDto.setDocumentItem(documentItem.getItemNo());
                    inventoryReceiveDto.setSite(documentItem.getSite());
                    inventoryReceiveDto.setItem(documentItem.getItem());
                    inventoryReceiveDto.setUnitOfMeasure(documentItem.getUnitOfMeasure());
                    inventoryReceiveDto.setCentralStorageLocation(documentItem.getErpStorageLocation());
                    inventoryReceiveDto.setStorageLocation(documentItem.getStorageLocation());
                    inventoryReceiveDto.setQty(Double.valueOf(documentItem.getQuantity()));
                    inventoryReceiveDto.setExpireDate(documentItem.getExpireDate());
                    inventoryReceiveDto.setBatchNumber(documentItem.getBatchNumber());
                    inventoryReceiveDto.setVendorId(documentItem.getVendorId());
                    inventoryReceiveDto.setVendor(documentItem.getVendor());
                    inventoryReceiveDto.setVendorLot(documentItem.getVendorLot());
                    inventoryReceiveDto.setMinPackQty(Double.valueOf(documentItem.getMinPackQty()));
                    return inventoryReceiveDto;
                })
                .collect(Collectors.toList());

        try {
            inventoryReceiveService.receive(inventoryReceiveDtos);
        } catch (Exception e) {
            response.setStatus(ResponseStatus.E);
            response.setMessage(e.getMessage());
        }

        return response;
    }
}
